#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Software : PyCharm
# @Time    : 2025-01-21 10:10
# @Author  : gzh
# @File : button_image_auto_size.py

"""
Button组件添加图片后会有两个问题第一个将图片作为属性或者公共变量保存起来否则会被回收，该组件对Button进行了重写，使用的时候仅需传入图片路径，及其他参数即可
"""

from . import Button, Image, ImageTk

class ButtonImageAutoSize(Button):
    def __init__(self, bg_path, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.image = Image.open(bg_path)
        self.image_c = None
        self.bind("<Configure>", lambda event: self.on_resize())

    def on_resize(self):
        self.image_c = self.image.resize((self.winfo_width(), self.winfo_height()))
        self.image_c = ImageTk.PhotoImage(self.image_c)
        self.configure(image=self.image_c)
